//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ehl.capture.webservice;

import com.ehl.capture.bean.CarBean;
import com.ehl.capture.db.Config;
import com.ehl.capture.tool.DateUtils;
import java.net.URL;
import javax.xml.rpc.ParameterMode;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.log4j.Logger;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;

public class AxisProxyClient {
    private static final Logger logger = Logger.getLogger(AxisProxyClient.class);
    private static int CONNECTION_TIMEOUT = Integer.parseInt(Config.getValue("sendcar.webservice.timeout"));

    public AxisProxyClient() {
    }

    public static boolean initTgs(CarBean bean) {
        Object[] paramObj = createRegistParamObj(bean);
        return initTgs(paramObj);
    }

    private static boolean initTgs(Object[] paramObj) {
        try {
            String url = Config.getValue("wxsurl");
            Call call = getRegistCallInstance(url);
            Long result = (Long)call.invoke(paramObj);
            if (result > 0L) {
                logger.info("初始化卡口成功");
                return true;
            } else {
                logger.info("初始化卡口失败:" + result + " 原因：" + WxsError.getErrMsg(result));
                return false;
            }
        } catch (Exception var4) {
            logger.error("注册卡口失败", var4);
            var4.printStackTrace();
            return false;
        }
    }

    private static Object[] createRegistParamObj(String kkbh, String fxlx, String cdh) {
        Object[] obj = new Object[]{kkbh, fxlx, Long.parseLong(cdh), getInfo()};
        return obj;
    }

    private static Object[] createRegistParamObj(CarBean bean) {
        return createRegistParamObj(bean.getKkbh(), bean.getFxlx(), bean.getCdh());
    }

    public static boolean sendCar(CarBean bean) {
        try {
            String url = Config.getValue("wxsurl");
            Call call = getCallInstance(url);
            Object[] paramObj = createParamObj(bean);
            Long result = (Long)call.invoke(paramObj);
            if (result > 0L) {
                logger.info("上传数据成功,号牌号码:" + bean.getHphm() + " 过车时间:" + bean.getGcsj() + " 卡点编号:" + bean.getKkbh() + " tplj:" + bean.getTplj() + " tp1:" + bean.getTp1());
            } else {
                logger.error("上传数据失败:" + result + " " + WxsError.getErrMsg(result) + " KKBH = " + bean.getKkbh());
                if (result == -102L) {
                    callRegistProcess(bean);
                }

                if (isConnectionNotInitialize(result)) {
                    callRegistProcess(bean);
                }
            }

            return true;
        } catch (Exception var5) {
            logger.error("上传无锡所失败", var5);
            var5.printStackTrace();
            return false;
        }
    }

    private static void callRegistProcess(CarBean bean) {
        boolean initResult = initTgs(bean);
        if (initResult) {
            sendCar(bean);
        }

    }

    private static boolean isConnectionNotInitialize() {
        return isConnectionNotInitialize();
    }

    private static boolean isConnectionNotInitialize(Long result) {
        return result == -24L;
    }

    private static Object[] createParamObj(CarBean bean) {
        Object[] obj = new Object[]{bean.getKkbh(), bean.getFxlx(), Long.parseLong(bean.getCdh()), bean.getHphm(), bean.getHpzl(), bean.getGcsj(), Long.parseLong(bean.getClsd()), Long.parseLong(bean.getClxs()), bean.getWflx(), -1, bean.getHpys(), bean.getHpzl().equals("01") ? "1" : "0", "", "", "", "", "", "", bean.getTplj(), bean.getTp1(), bean.getTp2(), bean.getTp3(), ""};
        return obj;
    }

    private static Call getRegistCallInstance(String url) throws Exception {
        Service service = new Service();
        Call call = (Call)service.createCall();
        call.setTimeout(CONNECTION_TIMEOUT);
        call.addParameter("kkbh", XMLType.XSD_ANYTYPE, ParameterMode.IN);
        call.addParameter("fxlx", XMLType.XSD_ANYTYPE, ParameterMode.IN);
        call.addParameter("cdh", XMLType.XSD_ANYTYPE, ParameterMode.IN);
        call.addParameter("info", XMLType.XSD_ANYTYPE, ParameterMode.IN);
        call.setReturnType(XMLType.XSD_LONG);
        call.setOperationName("InitTrans");
        call.setTargetEndpointAddress(new URL(url.trim()));
        return call;
    }

    private static Call getCallInstance(String url) throws Exception {
        Service service = new Service();
        Call call = (Call)service.createCall();
        call.setTimeout(CONNECTION_TIMEOUT);
        call.addParameter("kkbh", XMLType.XSD_ANYTYPE, ParameterMode.IN);
        call.addParameter("fxlx", XMLType.XSD_ANYTYPE, ParameterMode.IN);
        call.addParameter("cdh", XMLType.XSD_ANYTYPE, ParameterMode.IN);
        call.addParameter("hphm", XMLType.XSD_ANYTYPE, ParameterMode.IN);
        call.addParameter("hpzl", XMLType.XSD_ANYTYPE, ParameterMode.IN);
        call.addParameter("gcsj", XMLType.XSD_ANYTYPE, ParameterMode.IN);
        call.addParameter("clsd", XMLType.XSD_ANYTYPE, ParameterMode.IN);
        call.addParameter("clxs", XMLType.XSD_ANYTYPE, ParameterMode.IN);
        call.addParameter("wfdm", XMLType.XSD_ANYTYPE, ParameterMode.IN);
        call.addParameter("cwkc", XMLType.XSD_ANYTYPE, ParameterMode.IN);
        call.addParameter("hpys", XMLType.XSD_ANYTYPE, ParameterMode.IN);
        call.addParameter("cllx", XMLType.XSD_ANYTYPE, ParameterMode.IN);
        call.addParameter("fzhpzl", XMLType.XSD_ANYTYPE, ParameterMode.IN);
        call.addParameter("fzhphm", XMLType.XSD_ANYTYPE, ParameterMode.IN);
        call.addParameter("fzhpys", XMLType.XSD_ANYTYPE, ParameterMode.IN);
        call.addParameter("clpp", XMLType.XSD_ANYTYPE, ParameterMode.IN);
        call.addParameter("clwx", XMLType.XSD_ANYTYPE, ParameterMode.IN);
        call.addParameter("csys", XMLType.XSD_ANYTYPE, ParameterMode.IN);
        call.addParameter("tplj", XMLType.XSD_ANYTYPE, ParameterMode.IN);
        call.addParameter("tp1", XMLType.XSD_ANYTYPE, ParameterMode.IN);
        call.addParameter("tp2", XMLType.XSD_ANYTYPE, ParameterMode.IN);
        call.addParameter("tp3", XMLType.XSD_ANYTYPE, ParameterMode.IN);
        call.addParameter("tztp", XMLType.XSD_ANYTYPE, ParameterMode.IN);
        call.setReturnType(XMLType.XSD_LONG);
        call.setOperationName("WriteVehicleInfo");
        call.setTargetEndpointAddress(new URL(url.trim()));
        return call;
    }

    private static String getServiceUrl(String wxsurl) {
        return wxsurl.indexOf("?") > 0 ? wxsurl.substring(0, wxsurl.lastIndexOf("?")) : wxsurl;
    }

    public static String getInfo() {
        Element root = new Element("info");
        Element jkid = new Element("jkid");
        jkid.addContent(Config.getValue("jkid"));
        root.addContent(jkid);
        Element jkxlh = new Element("jkxlh");
        jkxlh.addContent(Config.getValue("jkxlh"));
        root.addContent(jkxlh);
        Element time = new Element("time");
        time.addContent(DateUtils.getNowTime());
        root.addContent(time);
        Document doc = new Document(root);
        XMLOutputter out = new XMLOutputter();
        String xmlStr = out.outputString(doc);
        logger.info("辅助信息：" + xmlStr);
        return xmlStr;
    }
}
